package {

	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import org.papervision3d.core.math.Number3D;
	import org.papervision3d.materials.MovieMaterial;
	import org.papervision3d.objects.parsers.DAE;
	import org.papervision3d.objects.primitives.Plane;
	
	[SWF(width=640, height=480, backgroundColor=0x808080, frameRate=30)]
	
	public class mxr3d extends PV3DARApp {
		
		private var _earth:DAE;
		private var _cow:DAE;
		private var p:Plane;
		private var pDirection:Boolean;
		private var mc:MovieClip;
        private	var txt:TextField;
		
		public function mxr3d() {
			addEventListener(Event.INIT, _onInit);
			init('Data/camera_para.dat', 'Data/flarlogo.pat');
		}
		
		private function _onInit(e:Event):void {
			_earth = new DAE();
			_earth.load('Models/cow.dae');
			//_earth.load('Models/neuro.dae');
			//_earth.load('Models/earth.dae');
			_earth.scale = 50;
			_earth.rotationX = 90;
			_markerNode.addChild(_earth);
			
			addEventListener(Event.ENTER_FRAME, _update);
			mirror = true;
			stage.addEventListener(KeyboardEvent.KEY_DOWN, _onKeyDown);
			
			mc = new MovieClip();
        	txt = new TextField();
        	txt.wordWrap = true;
        	txt.width = 300;
        	txt.height = 120;
        	txt.multiline = true;
        	txt.htmlText= "Vache Folle<br>Scanning...";
        	
        	txt.autoSize = TextFieldAutoSize.LEFT;

        	txt.setTextFormat(new TextFormat("Arial", 48));
        	
        	txt.antiAliasType = AntiAliasType.ADVANCED;
        
        	mc.graphics.beginFill(0x538C59,0.3);
        	mc.graphics.drawRect(0,0,300,120);
        	mc.graphics.endFill();
        	mc.addChild(txt);
        	
        	var mat:MovieMaterial = new MovieMaterial(mc, true, false, true);
        	mat.doubleSided = true;
        	mat.smooth = true;
        	mat.tiled = true;
        	
        	
        	p = new Plane(mat, 300, 120);
        	p.rotationX=90;
        	p.y=45;
        	
        	p.x=50
        	p.z = 50;
        	_markerNode.addChild(p);
        	
        	pDirection = true;
		
			
		}
		
		private function _update(e:Event):void {
			_earth.rotationZ -= 5
			//trace(p.sceneY);
			
			
			if(pDirection)
				p.y -= 8;
			else
				p.y += 8;
				
			if(p.sceneY < -80){
				pDirection = false;
			}
			if(p.sceneY > 50){
				pDirection = true;
			}

		}
		
		private function _onKeyDown(e:KeyboardEvent):void {
			
				trace(e.keyCode);
			switch(e.keyCode) {
        	case 38: //Up
            	_earth.translate(10,new Number3D(1,0,0));
        		break;
		    case 39: //
            	_earth.translate(10,new Number3D(0,1,0));
            	break;
        	case 40:
        		_earth.translate(10,new Number3D(-1,0,0));
        		break;
        	case 37:
        		_earth.translate(10,new Number3D(0,-1,0));
        		break;
        			
			}
		}
	}
}
